#include "modeSetWin.h"
#include "usart.h"
#include "rsRecv_dirver.h"

/*创建一个键盘事件组*/
static lv_group_t* keyGroup;

/*创建一个标签显示状态选项*/
static lv_obj_t* select_lable;

static uint8_t select_index;

/*创建一个结构体用来存放选项文字*/
const char *myOption[] = {"旋钮控制","IO控制","串口控制","旋钮控制(PID)","模拟控制"};

/**退出本界面回调函数**/
static void exit_screen(void* scr)
{
	lv_obj_del(scr);
}

/**"Enter"按键回调函数**/
static void enter_btn_cb(lv_event_t * e)
{
	//保存并退出
	motor_set_CtrlMode(select_index);
    if(select_index == 2)
    {   
        uint8_t clear;
        HAL_NVIC_DisableIRQ(EXTI9_5_IRQn);
        HAL_UART_Receive(&huart1,&clear,1,100);
        //clear =(uint8_t)(huart1.Instance->DR & (uint8_t)0x00FF);
        HAL_UART_Receive_IT(&huart1, (uint8_t *)RxBuff, 1); //打开串口中断接收
    }
    else if(select_index == 0)
    {
        HAL_NVIC_DisableIRQ(EXTI9_5_IRQn);
    }
    else if(select_index == 1)
    {
        HAL_NVIC_EnableIRQ(EXTI9_5_IRQn);
    }
	lv_group_del(keyGroup);
	lv_group_set_default(NULL);
	lv_async_call(exit_screen, lv_scr_act());
	menu(2);
}

/**"Cancel"按钮回调函数**/
static void cancel_btn_cb(lv_event_t * e)
{
	lv_group_del(keyGroup); //不保存退出
	lv_group_set_default(NULL);
	lv_async_call(exit_screen, lv_scr_act());
	menu(2);
}

/**下拉框回调**/
static void dd_event_handler(lv_event_t * e)
{
    switch (lv_indev_get_key(lv_indev_get_act()))
    {
    case LV_KEY_RIGHT:
        if(select_index==2)
            select_index = 0;  
        else
            select_index ++;
        break;
    case LV_KEY_LEFT:
        if(select_index == 0)
            select_index = 2;  
        else
            select_index --;
        break;
    default:
        break;
    }
   lv_label_set_text(select_lable,myOption[select_index]); 
}
static void dd_focued_cb(lv_event_t * e)
{
    if(LV_EVENT_FOCUSED == lv_event_get_code(e))
    {
        lv_obj_set_style_outline_width(select_lable,1,LV_PART_MAIN);
        lv_obj_set_style_outline_color(select_lable,lv_palette_main(LV_PALETTE_BLUE),LV_PART_MAIN);
        lv_obj_set_style_outline_pad(select_lable,2,LV_PART_MAIN);        
    }
    else
    {
        lv_obj_set_style_outline_width(select_lable,0,LV_PART_MAIN);
    }
}


void open_modeSetWin()
{
    lv_obj_t*  scr = lv_obj_create(NULL);
	lv_scr_load(scr);

    select_lable = lv_label_create(lv_scr_act());
    lv_obj_set_style_text_font(select_lable,&yahei_18,LV_PART_MAIN);
    lv_obj_center(select_lable);
    select_index = motor_get_CtrlMode();
    lv_label_set_text(select_lable,myOption[select_index]);
    /*对中间的选项标签的风格设置*/
    lv_obj_set_width(select_lable,140);
    lv_obj_set_style_bg_opa(select_lable,LV_OPA_COVER,LV_PART_MAIN);
    lv_obj_set_style_text_align(select_lable,LV_TEXT_ALIGN_CENTER,LV_PART_MAIN);
    lv_obj_set_style_radius(select_lable,5,LV_PART_MAIN);
    lv_obj_set_style_border_color(select_lable,lv_color_hex(0xDDDDDD),LV_PART_MAIN);
    lv_obj_set_style_border_side(select_lable,LV_BORDER_SIDE_FULL,LV_PART_MAIN);
    lv_obj_set_style_border_width(select_lable,1,LV_PART_MAIN);
    /*为标签添加事件*/
    lv_obj_add_event_cb(select_lable,dd_event_handler,LV_EVENT_KEY,NULL);
    lv_obj_add_event_cb(select_lable,dd_focued_cb,LV_EVENT_FOCUSED,NULL);
    lv_obj_add_event_cb(select_lable,dd_focued_cb,LV_EVENT_DEFOCUSED,NULL);
    //lv_obj_add_flag(select_lable,LV_OBJ_FLAG_CLICK_FOCUSABLE);

    //给中间选项标签添左右添加 '<' '>'图片
    lv_obj_t* label_1 = lv_label_create(lv_scr_act());
    lv_obj_align_to(label_1,select_lable,LV_ALIGN_OUT_RIGHT_MID,-11,0);
    lv_label_set_text(label_1,LV_SYMBOL_RIGHT);
    lv_obj_t* label_2 = lv_label_create(lv_scr_act());
    lv_obj_align(label_2,LV_ALIGN_CENTER,-65,0);
    lv_label_set_text(label_2,LV_SYMBOL_LEFT);

    lv_obj_t* label_title = lv_label_create(lv_scr_act()); //标题标签
	lv_obj_set_width(label_title, 160);
	lv_obj_set_style_text_font(label_title, &yahei_16, 0);
	lv_obj_add_style(label_title, &style_label_3, LV_PART_MAIN);
	lv_obj_align(label_title, LV_ALIGN_TOP_MID, 0, 0);
	lv_label_set_text(label_title, "调节方式");


    lv_obj_t* btn1 = lv_btn_create(lv_scr_act());
	lv_obj_t* label = lv_label_create(btn1);
	lv_label_set_text(label, "Enter");
	lv_obj_align_to(label, btn1, LV_ALIGN_CENTER, 0, -5);
	lv_obj_set_size(btn1, LV_SIZE_CONTENT, 16);
	lv_obj_align(btn1, LV_ALIGN_BOTTOM_MID, -40, -3);
	lv_obj_add_event_cb(btn1, enter_btn_cb, LV_EVENT_PRESSED, NULL);

	lv_obj_t* btn2 = lv_btn_create(lv_scr_act());
	label = lv_label_create(btn2);
	lv_label_set_text(label, "Cancel");
	lv_obj_align_to(label, btn2, LV_ALIGN_CENTER, 0, -5);
	lv_obj_set_size(btn2, LV_SIZE_CONTENT, 16);
	lv_obj_align(btn2, LV_ALIGN_BOTTOM_MID, 40, -3);
	lv_obj_add_event_cb(btn2, cancel_btn_cb, LV_EVENT_PRESSED, NULL);

	keyGroup = lv_group_create();
	lv_indev_set_group(indev_keypad, keyGroup);
	lv_group_set_default(keyGroup);

	lv_group_add_obj(keyGroup, select_lable);
	lv_group_add_obj(keyGroup, btn1);
	lv_group_add_obj(keyGroup, btn2);
}

